package jsyf.oa.service.wechat.service_account;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.facade.wechat.config.WeChatConst;
import jsyf.oa.facade.wechat.service_account.AnalyzeDataFacade;
import jsyf.oa.facade.wechat.service_company.TokenFacade;
import jsyf.oa.service.wechat.service_company.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class AnalyzeDataService implements AnalyzeDataFacade {

    @Autowired
    private TokenFacade tokenFacade;

    @Override
    public JSONObject getusersummary(String beginTime, String endTime) {

        // 数据库中获取access_token
        String accessToken = tokenFacade.getServiceAccessToken(WeChatConst.weChat_account_type);
        //  拼装获取用户增减数据的url
        String url = WeChatConst.get_user_summary.replace("ACCESS_TOKEN", accessToken);
        //  拼装请求参数
        //  json字符串转Map
        Map map = (Map) JSON.parse("{}");
        map.put("begin_date", beginTime);
        map.put("end_date", endTime);
        String json = map.toString();
        //  调用接口
        JSONObject jsonObject = HttpUtil.request(url, "POST", json);

        return jsonObject;
    }

    @Override
    public JSONObject getusercumulate(String beginTime, String endTime) {

        // `数据库中获取access_token
        String accessToken = tokenFacade.getServiceAccessToken(WeChatConst.weChat_account_type);
        //  拼装url
        String url = WeChatConst.get_user_cumulate.replace("ACCESS_TOKEN", accessToken);
        //  拼装请求参数
        Map map = (Map) JSON.parse("{}");
        map.put("begin_date", beginTime);
        map.put("end_date", endTime);
        String json = map.toString();
        //  调用接口
        JSONObject jsonObject = HttpUtil.request(url, "POST", json);

        return jsonObject;
    }
}
